package org.zoomdev.zoom.dao.impl;

import org.zoomdev.zoom.dao.DatabaseConfig;
import org.zoomdev.zoom.dao.RenameStrategy;
import org.zoomdev.zoom.dao.RenameStrategyFactory;
import org.zoomdev.zoom.dao.rename.CamelRenameStrategyFactory;
import org.zoomdev.zoom.dao.rename.DetectPrefixStrategyFactory;
import org.zoomdev.zoom.dao.rename.RawRenameStrategyFactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DatabaseConfigImpl implements DatabaseConfig {

    /**
     * 这个在一开始初始化的时候确定
     */
    private final Map<String, RenameStrategyFactory> map = new ConcurrentHashMap<>();

    private final Map<String, String> nameMap = new ConcurrentHashMap<>();

    private String strategy = RenameStrategy.PREFIX_CAMERA;


    public DatabaseConfigImpl() {
        //默认策略
        this.map.put(RenameStrategy.PREFIX_CAMERA, new DetectPrefixStrategyFactory());
        this.map.put(RenameStrategy.CAMEL, new CamelRenameStrategyFactory());
        this.map.put(RenameStrategy.RAW, new RawRenameStrategyFactory());

    }

    @Override
    public void setTableLocalName(String databaseTableName, String localTableName) {
        nameMap.put(databaseTableName, localTableName);
    }

    @Override
    public String getTableLocalName(String name) {
        return nameMap.get(name);
    }

    @Override
    public RenameStrategyFactory getColumnRenameStrategy() {
        return map.get(strategy);
    }

    @Override
    public void setColumnRenameStrategy(String strategy) {
        this.strategy = strategy;
    }

    @Override
    public void registerColumnRenameStrategy(String strategy, RenameStrategyFactory factory) {
        map.put(strategy, factory);
    }

    @Override
    public RenameStrategyFactory getColumnRenameStrategy(String strategy) {
        return map.get(strategy);
    }
}
